package com.jcfk.eam.controller.csm;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.*;
import com.jcfk.eam.domain.dto.csm.CsmLedgerManageDTO;
import com.jcfk.eam.domain.query.csm.CsmLedgerManageQuery;
import com.jcfk.eam.service.csm.CsmLedgerManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 消耗品台账管理
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-11-04
 */
@Api(tags = {"消耗品台账管理管理"})
@RestController
@RequestMapping("csmledgermanage")
public class CsmLedgerManageController {
    @Autowired
    private CsmLedgerManageService csmLedgerManageService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<CsmLedgerManageQuery>", name = "requestParam", value = "", required = true)})
    @ApiOperation("分页查询消耗品台账管理")
    @PostMapping("/queryByPage")
    public RetPage<CsmLedgerManageDTO> queryByPage(@RequestBody ReqPageParam<CsmLedgerManageQuery> requestParam) {

        // 设置默认排序
        if (requestParam.getSort().getOrderItem() == null) {
            ReqSortParam<CsmLedgerManageQuery> sortParam = new ReqSortParam();
            sortParam.setColumn("lastUpdateTime");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }
        DataPage<CsmLedgerManageDTO> page = new DataPage<CsmLedgerManageDTO>((long) requestParam.getCurrent(), (long) requestParam.getSize(), requestParam.getSort().getOrderItem(), CsmLedgerManageDTO.class);
        page = csmLedgerManageService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 消耗品台账管理
     * 根据id 查询详情
     */
    @ApiOperation("查询消耗品台账管理")
    @GetMapping("/get/{enabled}")
    public RetResult<CsmLedgerManageDTO> get(@PathVariable("enabled") String enabled) {
        CsmLedgerManageDTO item = csmLedgerManageService.get(enabled);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理消耗品台账管理
     */
    @ApiOperation("保存消耗品台账管理")
    @PostMapping("/save")
    @RequestLogHeader(modular = "消耗品台账管理管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid CsmLedgerManageDTO csmLedgerManage) {
        // 数据校验
        RetResult ret = csmLedgerManageService.isValid(csmLedgerManage);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(csmLedgerManage.getId())) {
            // 新增
            csmLedgerManageService.insert(csmLedgerManage);
        } else {
            // 更新
            csmLedgerManageService.update(csmLedgerManage);
        }
        return RetResult.ok();

    }

    /**
     * 批量删除消耗品台账管理
     */
    @ApiOperation("批量删除消耗品台账管理")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "消耗品台账管理管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody CsmLedgerManageDTO csmLedgerManage) {
        return RetResult.ok(csmLedgerManageService.logicDeleteList(csmLedgerManage));
    }


    /**
     * 删除消耗品台账管理
     */
    @ApiOperation("删除消耗品台账管理")
    @PostMapping("/deleteItem/{enabled}")
    @RequestLogHeader(modular = "消耗品台账管理管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("enabled") String enabled) {
        csmLedgerManageService.deleteItem(enabled);
        return RetResult.ok();
    }
}
